﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using OznerOA.BLL;
using OznerOA.Common;

namespace OznerOA.UI
{
    public partial class FrmIndex : FrmBase
    {
        UserBLL userBLL = UserBLL.GetInstance();
        #region 构造函数
        public FrmIndex()
        {
            InitializeComponent();
            this.Load += FrmIndex_Load;
        }
        #endregion
        #region 窗体加载事件
        private void FrmIndex_Load(object sender, EventArgs e)
        {
            if (userBLL.LoginUser != null)
            {
                ShowWaiting("正在获取用户信息!");
                initInfo();
                SetPad();
                CloseWaiting();
                txtDept.Focus();
                txtPad.LostFocus += TxtPad_LostFocus;
            }
        }

        private void TxtPad_LostFocus(object sender, EventArgs e)
        {
            logBLL.Info("保存便签中...");
            string padContent = txtPad.Text.Trim();
            //使用线程保存便签
            ThreadHelper.StartNewThread(() =>
            {
                BaseResult<string> br = userBLL.SavePad(padContent);
                if (br.Code == Code.OK && padContent.Equals(br.Object))
                {
                    logBLL.Info("便签保存成功!");
                    logBLL.Debug("保存内容:" + br.Object);
                }
                else
                {
                    logBLL.Error("便签保存失败," + br.Code.ToString() + ":" + br.Message);
                }
            });
        }
        #endregion
        #region 设置便签
        private void SetPad()
        {
            BaseResult<string> br = userBLL.GetPad();
            if(br.Code == Code.OK)
            {
                txtPad.Text = br.Object;
            }
        }
        #endregion
        #region 设置用户信息
        private void initInfo()
        {
            if (userBLL.LoginUser.Sex.Contains("男"))
            {
                pictureEdit1.Image = UI.Properties.Resources.man_wev8;
            }
            else
            {
                pictureEdit1.Image = UI.Properties.Resources.women_wev8;
            }
            lblName.Text = userBLL.LoginUser.Name;
            lblSex.Text = string.Format("（{0}）", userBLL.LoginUser.Sex);
            lblUserId.Text = userBLL.LoginUser.Id;
            txtDept.Text = userBLL.LoginUser.Dept;
            txtShip.Text = userBLL.LoginUser.Ship;
            txtManager.Text = userBLL.LoginUser.ManagerName;
            txtSub.Text = userBLL.LoginUser.Subsection;
            txtMail.Text = userBLL.LoginUser.Email;
            txtPhone.Text = userBLL.LoginUser.Phone;
            txtStatus.Text = userBLL.LoginUser.Status;
            txtTel.Text = userBLL.LoginUser.Tel;
        }
        #endregion
    }
}